<!DOCTYPE html>
<html lang="en">
	<head>
		<title>threejs webgl - node material - custom size hdr environment mapping</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #fff;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #000;

				margin: 0px;
				overflow: hidden;
			}
			a { color: #00f }

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}
		</style>
	</head>
	<body>

		<div id="container"></div>
		<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - High dynamic range (RGBE) Image-based Lighting (IBL)<br />using run-time generated pre-filtered roughness mipmaps (PMREM)<br/>
			Created by Prashant Sharma and <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.</div>

		<script type="module">

			import * as THREE from '../build/three.module.js';

			import Stats from './jsm/libs/stats.module.js';

			import { GUI } from './jsm/libs/dat.gui.module.js';
			import { OrbitControls } from './jsm/controls/OrbitControls.js';
			import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';

			import {
				StandardNodeMaterial,
				FloatNode,
				OperatorNode,
				TextureNode,
				TextureCubeNode
			} from './jsm/nodes/Nodes.js';

			const params = {
				roughness: 0.0,
				metalness: 0.0,
				exposure: 1.0,
				intensity: 1.0,
				animate: true,
				debug: false
			};

			let container, stats;
			let camera, scene, renderer, controls;
			let nodeMaterial, nodeTexture, nodeTextureIntensity, torusMesh, planeMesh;
			let hdrCubeRenderTarget;
			let hdrCubeMap;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.set( 0, 0, 120 );

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x000000 );

				renderer = new THREE.WebGLRenderer();

				//

				const geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );

				nodeMaterial = new StandardNodeMaterial();
				nodeMaterial.color.value = new THREE.Color( 0xffffff );
				nodeMaterial.roughness.value = params.metalness;
				nodeMaterial.metalness.value = params.roguhness;
				nodeMaterial.visible = false;

				nodeTexture = new TextureNode();
				nodeTextureIntensity = new FloatNode( 1 );

				nodeMaterial.environment = new OperatorNode( new TextureCubeNode( nodeTexture ), nodeTextureIntensity, OperatorNode.MUL );

				torusMesh = new THREE.Mesh( geometry, nodeMaterial );
				scene.add( torusMesh );

				planeMesh = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial() );
				planeMesh.position.y = - 50;
				planeMesh.rotation.x = - Math.PI * 0.5;
				scene.add( planeMesh );

				const hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
				hdrCubeMap = new HDRCubeTextureLoader()
					.setPath( './textures/cube/pisaHDR/' )
					.setDataType( THREE.UnsignedByteType )
					.load( hdrUrls, function () {

						hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
						pmremGenerator.dispose();

						nodeTexture.value = hdrCubeRenderTarget.texture;

						hdrCubeMap.magFilter = THREE.LinearFilter;
						hdrCubeMap.needsUpdate = true;

						planeMesh.material.map = hdrCubeRenderTarget.texture;
						planeMesh.material.needsUpdate = true;

						nodeMaterial.visible = true;

					} );

				const pmremGenerator = new THREE.PMREMGenerator( renderer );
				pmremGenerator.compileCubemapShader();

				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				renderer.toneMapping = THREE.ACESFilmicToneMapping;
				renderer.outputEncoding = THREE.sRGBEncoding;

				stats = new Stats();
				container.appendChild( stats.dom );

				controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = 50;
				controls.maxDistance = 300;

				window.addEventListener( 'resize', onWindowResize );

				const gui = new GUI();

				gui.add( params, 'roughness', 0, 1, 0.01 );
				gui.add( params, 'metalness', 0, 1, 0.01 );
				gui.add( params, 'exposure', 0, 2, 0.01 );
				gui.add( params, 'intensity', 0, 2, 0.01 );
				gui.add( params, 'animate', true );
				gui.add( params, 'debug', false );
				gui.open();

			}

			function onWindowResize() {

				const width = window.innerWidth;
				const height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				renderer.setSize( width, height );

			}

			function animate() {

				requestAnimationFrame( animate );

				stats.begin();
				render();
				stats.end();

			}

			function render() {

				torusMesh.material.roughness.value = params.roughness;
				torusMesh.material.metalness.value = params.metalness;

				nodeTextureIntensity.value = params.intensity;

				if ( params.animate ) {

					torusMesh.rotation.y += 0.005;

				}

				planeMesh.visible = params.debug;

				scene.background = hdrCubeMap;
				renderer.toneMappingExposure = params.exposure;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
